package com.gxsyh.nantie.user.management.provider.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxsyh.nantie.commons.configure.rabbitmq.RabbitMQConfig;
import com.gxsyh.nantie.commons.dto.PageHelpDTO;
import com.gxsyh.nantie.commons.dto.ResponseResult;
import com.gxsyh.nantie.commons.dto.SendLogResult;
import com.gxsyh.nantie.commons.utils.OkHttpClientUtil;
import com.gxsyh.nantie.user.management.provider.controller.dto.InsterDepartmentParam;
import com.gxsyh.nantie.user.management.provider.domain.TbDepartment;
import com.gxsyh.nantie.user.management.provider.domain.UserInfoList;
import com.gxsyh.nantie.user.management.provider.service.TbDepartmentService;
import com.gxsyh.nantie.user.management.provider.service.UserInfoListService;
import com.gxsyh.nantie.user.management.provider.service.UserInfoService;
import okhttp3.ResponseBody;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @email yom535@outlook.com
 * @author: 刘锋嘉(Liu Fengjia)
 * @date: 2020/1/14
 * @time: 11:08
 * @Description:
 */
@Transactional
@RestController
public class ApartmentController {
    @Resource
    TbDepartmentService tbDepartmentService;

    @Resource
    UserInfoListService userInfoListService;

    @Resource
    AmqpTemplate amqpTemplate;

    @Value("${base.config.leaf.url}")
    String LEAF_URL;

    /**
     * 通过自己公寓的ID查询子集的公寓
     * @param departmentId
     * @return
     */
    @GetMapping(value = "/user/apartment/subset")
    public ResponseResult<List<TbDepartment>> queryingSubsetDepartmentsId(@RequestParam Long departmentId){
        List<TbDepartment> tbDepartments = tbDepartmentService.queryingSubsetApartmentId(departmentId);
        return new ResponseResult<List<TbDepartment>>(HttpStatus.OK.value(),"查询公寓子部门",tbDepartments);
    }


    /**
     * 添加子部门
     * @param insterDepartmentParam
     * @param httpServletResponse
     * @return
     */
    @PostMapping(value = "/user/apartment/inster")
    public ResponseResult<Void> insterDepartment(@RequestBody InsterDepartmentParam insterDepartmentParam, HttpServletResponse httpServletResponse) throws IOException {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String userId = authentication.getName();
        if (insterDepartmentParam.getMainDepartmentId()==88)
        {
            httpServletResponse.setStatus(500);
            return new ResponseResult<>(HttpStatus.INTERNAL_SERVER_ERROR.value(),"此部门没有下级部门");
        }
        if (insterDepartmentParam.getAddedDepartment().isEmpty()){
            httpServletResponse.setStatus(500);
            return new ResponseResult<>(HttpStatus.INTERNAL_SERVER_ERROR.value(),"请检查输入格式");
        }
        ResponseBody body = OkHttpClientUtil.getInstance().getData(LEAF_URL + "insterdepartment").body();
        int i = tbDepartmentService.insterDepartment(Long.valueOf(body.string()),Long.valueOf(insterDepartmentParam.getMainDepartmentId()), insterDepartmentParam.getAddedDepartment(),1);
        if (i>0)
        {
            //                使用消息队列发送日志
            SendLogResult sendLogResult = new SendLogResult();
            sendLogResult.setUserId(Long.valueOf(userId));
            sendLogResult.setUsername(insterDepartmentParam.getAddedDepartment());
            sendLogResult.setInfo("向部门ID:"+insterDepartmentParam.getMainDepartmentId()+"添加部门:"+insterDepartmentParam.getAddedDepartment());
            amqpTemplate.convertAndSend(RabbitMQConfig.SEND_LOG_QUEUE,sendLogResult);
//                end消息队列发送日志

            return new ResponseResult<>(HttpStatus.OK.value(),"添加成功",null);
        }
        else {
            httpServletResponse.setStatus(500);
            return new ResponseResult<>(HttpStatus.INTERNAL_SERVER_ERROR.value(),"请检查输入格式");
        }
    }

}
